﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.WPF;
using System.Windows.Media.Imaging;
using System.Collections;
using System.Windows.Media;
using System.Windows;

namespace KocMapView
{
    class KocMapViewViewModel : ViewModelBase
    {
        #region declarations
        RenderTargetBitmap _RTB = null;
        ArrayList _KIs = null;
        KoordItem _KI = new KoordItem("");

        private String _strSeekPlayerName = "";
        private Int32 _intRadius = 15;
        private Boolean _fKoordsMarked = false;
        private Boolean _fAutoMarkPlayerCities = true;

        SolidColorBrush BR1 = null;
        SolidColorBrush BR2 = null;
        #endregion



        #region constructor / terminator
        public KocMapViewViewModel()
        {
            _RTB = new RenderTargetBitmap(750, 750, 96, 96, PixelFormats.Pbgra32);
            BR1 = Brushes.White;
            BR2 = new SolidColorBrush(Color.FromArgb(255, 245, 245, 245));

            DrawBackGround();
        }

        ~KocMapViewViewModel()
        {
            _RTB = null;
        }
        #endregion



        #region private functions
        public void DrawBackGround()
        {
            DrawingVisual dv = new DrawingVisual();
            using(DrawingContext dc = dv.RenderOpen())
            {
                SolidColorBrush BR = Brushes.Wheat;

                for(Int32 X=0; X < 750; X += 150)
                {
                    for(Int32 Y=0; Y < 750; Y += 150)
                    {
                        if(BR == BR1)
                        {
                            BR = BR2;
                        }
                        else
                        {
                            BR = BR1;
                        }

                        dc.DrawRectangle(BR, null, new Rect(X, Y, 150, 150));
                    }
                }

                dc.DrawRectangle(Brushes.Transparent, null, new Rect(300, 300, 150, 150));

                dc.Close();
            }
            _RTB.Render(dv);
        }

        private void _ShowCoords(
            ArrayList KoordItems)
        {
            DrawingVisual dv = new DrawingVisual();

            using(DrawingContext dc = dv.RenderOpen())
            {
                foreach(KoordItem KI in KoordItems)
                {
                    dc.DrawRectangle(KI.Color, null, new Rect((Double)KI.X, (Double)KI.Y, 1, 1));
                    //dc.DrawEllipse(null, new Pen(KI.Color, 1), new Point((Double)KI.X, (Double)KI.Y), 15, 15);
                }

                dc.Close();
            }
            
            _RTB.Render(dv);
        }

        private void _CheckCoords(
            Int32 X,
            Int32 Y)
        {
            if(_KIs != null)
            {
                _KI = new KoordItem("");

                foreach(KoordItem KI in _KIs)
                {
                    if(KI.X == X && KI.Y == Y)
                    {
                        _KI = KI;

                        break;
                    }
                }

                OnPropertyChanged("CurrentCoords");
                OnPropertyChanged("CurrentPlayer");
                OnPropertyChanged("CurrentMight");
                OnPropertyChanged("CurrentAlliance");
                OnPropertyChanged("CurrentCity");
                OnPropertyChanged("CurrentOnlineStatus");
                OnPropertyChanged("CurrentLastLogin");
            }
        }

        private void _MarkPlayer(
            String PlayerName)
        {
            if (_KIs != null)
            {
                ArrayList aryTemp = new ArrayList();

                foreach (KoordItem KI in _KIs)
                {
                    if (KI.PlayerName == PlayerName)
                    {
                        aryTemp.Add(KI);
                    }
                }

                if (aryTemp.Count != 0)
                {
                    DrawingVisual dv = new DrawingVisual();

                    using (DrawingContext dc = dv.RenderOpen())
                    {
                        foreach (KoordItem KI in aryTemp)
                        {
                            dc.DrawEllipse(null, new Pen(KI.Color, 1), new Point((Double)KI.X, (Double)KI.Y), _intRadius, _intRadius);
                        }

                        dc.Close();
                    }

                    _RTB.Render(dv);

                    _fKoordsMarked = true;
                }
                else
                {
                    if (_fKoordsMarked)
                    {
                        DrawBackGround();
                        _ShowCoords(_KIs);
                        _fKoordsMarked = false;
                    }
                }
            }
        }
        #endregion



        #region interface
        #region properties
        #region bitmap
        public RenderTargetBitmap MapViewBitmap
        {
            get
            {
                return _RTB;
            }
        }
        #endregion

        #region side data
        public String CurrentCoords
        {
            get
            {
                if (_KI.PlayerName != "")
                {
                    return _KI.X.ToString() + "," + _KI.Y.ToString();
                }
                return "";
            }
        }

        public String CurrentPlayer
        {
            get
            {
                return _KI.PlayerName;
            }
        }

        public String CurrentMight
        {
            get
            {
                return _KI.PlayerMight;
            }
        }

        public String CurrentAlliance
        {
            get
            {
                return _KI.Alliance;
            }
        }

        public String CurrentCity
        {
            get
            {
                return _KI.CityName;
            }
        }

        public String CurrentOnlineStatus
        {
            get
            {
                return _KI.OnlineStatus;
            }
        }

        public String CurrentLastLogin
        {
            get
            {
                return _KI.LastLogin;
            }
        }
        #endregion

        #region search and mark
        public String SeekPlayerName
        {
            get
            {
                return _strSeekPlayerName;
            }
            set
            {
                _strSeekPlayerName = value;
                _MarkPlayer(_strSeekPlayerName);
            }
        }

        public Boolean AutoMarkPlayerCities
        {
            get
            {
                return _fAutoMarkPlayerCities;
            }
            set
            {
                _fAutoMarkPlayerCities = value;
            }
        }

        public Int32 MarkRadius
        {
            get
            {
                return _intRadius;
            }
            set
            {
                if (value > 0 && value < 750)
                {
                    _intRadius = value;
                }
            }
        }
        #endregion
        #endregion

        #region methods
        public void ShowCoords(
            ArrayList KoordItems)
        {
            _KIs = KoordItems;
            _ShowCoords(_KIs);
        }

        public void CheckCoords(
            Int32 X,
            Int32 Y)
        {
            _CheckCoords(X, Y);
            if (_fAutoMarkPlayerCities)
            {
                _MarkPlayer(_KI.PlayerName);
            }
        }
        #endregion
        #endregion
    }
}
